/**
 * Copyright (C) 2010 MOBICOM. All rights reserved.
 * 
 * 
 * This software is the confidential and proprietary information of
 * Mobicom or one of its subsidiaries. You shall not disclose this
 * confidential information and shall use it only in accordance with
 * the terms of the license agreement or other applicable agreement
 * you entered into with Mobicom.
 * 
 * MOBICOM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. MOBICOM
 * SHALL NOT BE LIABLE FOR ANY LOSSES OR DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 * ITS DERIVATIVES.
 * 
 * Apr 12, 2011 11:17:03 PM
 * 
 * @author Nguyen Dinh Phuoc Duc(ndpduc@gmail.com)
 */
package com.lily.smsbox.helper;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;

import com.android.provider.Telephony.Sms;
import com.android.provider.Telephony.Sms.Conversations;
import com.android.provider.Telephony.Threads;
import com.lily.smsbox.model.Conversation;
import com.lily.smsbox.model.MessageThread;
import com.lily.smsbox.model.SmsMessage;

/**
 * Internal Class
 * 
 * @author Nguyen Dinh Phuoc Duc (ndpduc@gmail.com)
 * 
 */
public class SmsHelper {
  private Context _context;

  public SmsHelper(Context context) {
    _context = context;
  }

  public List<Conversation> getAllConversations() {
    ContentResolver contentResolver = _context.getContentResolver();
    Cursor c =
        contentResolver.query(
            Conversations.CONTENT_URI,
            null,
            null,
            null,
            Conversations.DEFAULT_SORT_ORDER);
    List<Conversation> conversations = new ArrayList<Conversation>();
    while (c.moveToNext()) {
      Conversation conversation = new Conversation();
      conversation.threadId =
          c.getInt(c.getColumnIndex(Conversations.THREAD_ID));
      conversation.snippet =
          c.getString(c.getColumnIndex(Conversations.SNIPPET));
      conversation.msgCount =
          c.getInt(c.getColumnIndex(Conversations.MESSAGE_COUNT));
      conversations.add(conversation);
    }
    ;
    c.close();
    return conversations;
  }

  public MessageThread getThreadById(long id) {
    ContentResolver contentResolver = _context.getContentResolver();
//    Cursor c =
//        contentResolver.query(
//            Threads.CONTENT_URI,
//            null,
//            Conversations.THREAD_ID + "=" + id,
//            null,
//            null);
    MessageThread thread = null;
//    if (c.moveToFirst()) {
//      thread = new MessageThread();
//      thread.id = id;
//      thread.address =
//          c.getString(c.getColumnIndex(Conversations.ADDRESS));
//      thread.date = c.getLong(c.getColumnIndex(Conversations.DATE));
//      thread.person =
//          c.getLong(c.getColumnIndex(Conversations.PERSON));
//      thread.personId =
//          c.getLong(c.getColumnIndex(Conversations.PERSON_ID));
//      thread.readStatus =
//          c.getInt(c.getColumnIndex(Conversations.READ));
//    }
//    c.close();
    return thread;
  }

  public List<SmsMessage> getSmsMessagesByThreadId(long threadId) {
    ContentResolver cr = _context.getContentResolver();
    Cursor c =
        Sms.query(cr, null, Sms.THREAD_ID + "=" + threadId, Sms.DATE
            + " ASC");
    List<SmsMessage> messages = new ArrayList<SmsMessage>();
    while (c.moveToNext()) {
      SmsMessage message = new SmsMessage();
      message.body = c.getString(c.getColumnIndex(Sms.BODY));
      message.type = c.getInt(c.getColumnIndex(Sms.TYPE));
      message.address = c.getString(c.getColumnIndex(Sms.ADDRESS));
      message.personId = c.getLong(c.getColumnIndex(Sms.PERSON_ID));
      messages.add(message);
    }
    return messages;
  }
}
